﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipSys.DataLayer;
using VipSys.SystemEntity;
using VipSys.Common.Library;
using VipSys.Business;
namespace VipSys
{
    public partial class HotelRegMag : System.Web.UI.Page
    {
        private List<User> m_lstUser;
        protected void Page_Load(object sender, EventArgs e)
        {
            SysAccount sysAccount = Session["SysAccount"] as SysAccount;
            if (sysAccount==null)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('登录超时，请重新登录!')</script>");
                return;
            }
            string action = Request["action"];
            if (!string.IsNullOrEmpty(action))
            {
                switch (action)
                {
                    case "BindGrid":
                        BindGrid();
                        return;
                    case "GetOrgnization":
                        GetOrgnization();
                        return;
                    case "BindRoleType":
                        BindRoleType();
                        return;
                    case "Add":
                        Add();
                        return;
                    case "Reg":
                        Reg();
                        return;
                    case "BindTemplate":
                        BindTemplate();
                        return;
                    case "BindEmployee":
                        BindEmployee();
                        return;
                    case "Export":
                        Export();
                        return;
                    case "Import":
                        Import();
                        return;
                    default:
                        return;
                }
            }
        }
        public void BindTemplate()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            json.success = true;
            List<SysCombo> lstTemplate = new List<SysCombo>();
            ComboAccess.Instance.GetTemplates(ref lstTemplate);
            foreach (var row in lstTemplate)
            {
                json.AddItem("value", row.Name);
                json.AddItem("name", row.Name);
                json.ItemOk();
            }
            json.totlalCount = lstTemplate.Count();
            if (json.totlalCount > 0)
            {
                jsons = json.ToString();
            }
            else
            {
                jsons = @"[]";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        public void BindEmployee()
        {
            SysAccount sysAccount = Session["SysAccount"] as SysAccount;
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            json.success = true;
            List<Employee> lstEmployee = new List<Employee>();
            EmployeeAccess.Instance.GetEmployeeList(ref lstEmployee);

            if (sysAccount.RoleType == SystemData.RoleType.SCHOOL)
            {
                string szEmployee = sysAccount.ID.ToString();
            }
            else
            {
                foreach (var row in lstEmployee)
                {
                    json.AddItem("value", row.ID.ToString());
                    json.AddItem("name", row.Name);
                    json.ItemOk();
                }
            }
            json.totlalCount = lstEmployee.Count();
            if (json.totlalCount > 0)
            {
                jsons = json.ToString();
            }
            else
            {
                jsons = @"[]";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        public void Export()
        {
            SysAccount sysAccount = Session["SysAccount"] as SysAccount;
            string jsons = "";
            JSONHelper jsonHelper = new JSONHelper();
            var Template = Request["Template"];
            var Name = Request["Name"];
            var szEmployeeID = Request["EmployeeID"];
            string filePath = string.Format("{0}{1}/{2}"
                , SystemParam.Instance.WorkPath
                , SystemData.FilePath.UPLOAD 
                , "test.xls");
            User query=new User();
            if (!string.IsNullOrEmpty(sysAccount.Name))
            {
                string szSchools = ViewAuthority.Instance.GetSchools(sysAccount.RoleType,sysAccount.OrgName);

                if (sysAccount.RoleType == SystemData.RoleType.SCHOOL)
                {
                    szEmployeeID = sysAccount.ID.ToString();
                    szSchools = "";
                }
                List<User> lstUser = new List<User>();
                short shRet = UserAccess.Instance.GetUserList(Name, szEmployeeID, ref lstUser, szSchools);
                try
                {

                    SysCombo template = new SysCombo();
                    short shRet2 = ComboAccess.Instance.GetTemplate(Template, ref template);
                    if (shRet2 != SystemData.ReturnValue.OK)
                        return;
                    string templateValue = template.Value;
                    shRet = HotelRegReport.Instance.Export(filePath, lstUser, templateValue);
                    if (shRet == SystemData.ReturnValue.OK)
                    {
                        jsons = "{\"success\":true,msg:\"導出成功\",filePath:\""
                            + string.Format("{0}/{1}"
                            , SystemData.FilePath.UPLOAD
                            , "test.xls")
                            + "\"}";
                    }
                    else
                    {
                        jsons = "{\"success\":true,msg:\"導出失敗\"}";
                    }
                }
                catch (Exception)
                {
                    jsons = "{\"success\":true,msg:\"導出失敗\"}";
                }

            }
            else
            {
                jsons = @"{success:false}";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        public void Import()
        {
            string jsons = "";
            var Template = Request["Template"];
            JSONHelper jsonHelper = new JSONHelper();
            var file = Request.Files[0];
            string filePath = string.Format("{0}//{1}/{2}"
             , SystemParam.Instance.WorkPath
             , SystemData.FilePath.UPLOAD
             , "test.xls");
          
            file.SaveAs(filePath);
            try
            {
                User query=new User();
                string szTemplateValue = string.Empty; // TODO: 初始化为适当的值
                SysCombo template = new SysCombo();
                short shRet2 = ComboAccess.Instance.GetTemplate(Template, ref template);
                if (shRet2 != SystemData.ReturnValue.OK)
                {
                    jsons = "{\"failure\":false,msg:\"導入失敗,请检查格式是否正确\"}";
                }
                else
                {
                    szTemplateValue = template.Value;
                    short shRet = HotelRegReport.Instance.Import(filePath, szTemplateValue);

                    System.IO.File.Delete(filePath);
                    //return Json(new object());
                    if (shRet == SystemData.ReturnValue.OK)
                    {
                        jsons = "{\"success\":true,msg:\"導入成功\"}";
                    }
                    else
                    {
                        jsons = "{\"failure\":false,msg:\"導入失敗\"}";
                    }
                }
            }
            catch (Exception ex)
            {
                jsons = "{\"failure\":false,msg:\"導入失敗,请检查格式是否正确.{"+ex.Message.ToString()+"}\"}";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void Add()
        {
            string jsons = "";
            User user = new User();
            var Name = Request["Name"];
            var Gender = Request["Gender"];
            var School = Request["School"];
            var ExamSchool = Request["ExamSchool"];
            var Sequence = Request["Sequence"];
            var Tel = Request["Tel"];
            var Bak = Request["Bak"];
            var RoleType = Request["RoleType"];
            var PassWord = Request["PassWord"];
            var PayMoney = Request["PayMoney"];
            var ExamPlace = Request["ExamPlace"];
            var Room = Request["Room"];
            var Hotel = Request["Hotel"];
            var HotelExpense = Request["HotelExpense"];
            var MoneyBack = Request["MoneyBack"];
            user.Name = Name;
            user.Gender = Gender;
            user.School = School;
            user.ExamSchool = ExamSchool;
            user.Sequence = Sequence;
            user.Tel = Tel;
            user.Bak = Bak;
            user.PassWord = PassWord;
            user.PayMoney = PayMoney;
            user.ExamPlace = ExamPlace;
            user.Room = Room;
            user.Hotel = Hotel;
            user.HotelExpense = HotelExpense;
            user.MoneyBack = MoneyBack;
            short shRet = UserAccess.Instance.Add(user);
            if (shRet != SystemData.ReturnValue.OK)
            {
                jsons = "{\"success\":true,msg:\"新增失败\"}";
            }
            else
            {
                jsons = "{\"success\":true,msg:\"新增成功\"}";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }

        private void Reg()
        {
            string jsons = "";
            User user = new User();
            var ID = Request["ID"];
            var Name = Request["Name"];
            var Gender = Request["Gender"];
            var School = Request["School"];
            var ExamSchool = Request["ExamSchool"];
            var Sequence = Request["Sequence"];
            var Tel = Request["Tel"];
            var Bak = Request["Bak"];
            var RoleType = Request["RoleType"];
            var PassWord = Request["PassWord"];
            var PayMoney = Request["PayMoney"];
            var ExamPlace = Request["ExamPlace"];
            var Room = Request["Room"];
            var Hotel = Request["Hotel"];
            var HotelExpense = Request["HotelExpense"];
            var MoneyBack = Request["MoneyBack"];
            
            user.ID = int.Parse(ID);
            UserAccess.Instance.GetUser(user, ref user);
            if (!string.IsNullOrEmpty(Bak))
            {
                user.Bak = Bak;
            }
            user.PayMoney = PayMoney;
            user.Room = Room;
            user.Hotel = Hotel;
            user.HotelExpense = HotelExpense;
            user.MoneyBack = MoneyBack;
            short shRet = UserAccess.Instance.Edit(user);
            if (shRet != SystemData.ReturnValue.OK)
            {
                jsons = "{\"success\":true,msg:\"编辑失败\"}";
            }
            else
            {
                jsons = "{\"success\":true,msg:\"编辑成功\"}";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        
        private void BindGrid()
        {
            SysAccount sysAccount = Session["SysAccount"] as SysAccount;
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            User query = new User();
            int start = int.Parse(Request.Form["start"]);
            int limit = int.Parse(Request.Form["limit"]);
            string szName = Request.Form["Name"];
            string szEmployeeID = Request.Form["EmployeeID"];
            if (!string.IsNullOrEmpty(sysAccount.Name))
            {
                if (query.Start != null && query.Limit != null)
                {
                    json.success = true;
                    string szSchools = ViewAuthority.Instance.GetSchools(sysAccount.RoleType,sysAccount.OrgName);
                    if (sysAccount.RoleType == SystemData.RoleType.SCHOOL)
                    {
                        szEmployeeID = sysAccount.ID.ToString();
                        szSchools = "";
                    }

                    short shRet = UserAccess.Instance.GetUserPageList(szName, szEmployeeID,sysAccount.RoleType, start, limit, ref this.m_lstUser, szSchools);
                    foreach (User item in this.m_lstUser)
                    {
                        json.AddItem("ID", item.ID.ToString());
                        json.AddItem("Bak", item.Bak);
                        json.AddItem("ExamPlace", item.ExamPlace);
                        json.AddItem("ExamSchool", item.ExamSchool);
                        json.AddItem("Gender", item.Gender);
                        json.AddItem("Hotel", item.Hotel);
                        json.AddItem("HotelExpense", item.HotelExpense);
                        json.AddItem("MoneyBack", item.MoneyBack);
                        json.AddItem("Name", item.Name);
                        json.AddItem("PassWord", item.PassWord);
                        json.AddItem("PayMoney", item.PayMoney);
                        json.AddItem("Room", item.Room);
                        json.AddItem("School", item.School);
                        json.AddItem("Sequence", item.Sequence);
                        json.AddItem("Tel", item.Tel);
                        json.AddItem("EmployeeID", item.EmployeeID);
                        json.AddItem("EmployeeName", item.EmployeeName);
                        json.AddItem("ExceptRoomie", item.ExceptRoomie);
                        json.AddItem("Template", item.Template);
                        json.AddItem("PayPlace", item.PayPlace);
                        json.ItemOk();
                    }
                    int totalCount = 0;
                    shRet = UserAccess.Instance.GetTotalCount(szName, szEmployeeID,sysAccount.ID.ToString(), ref totalCount, szSchools);
                    json.totlalCount = totalCount;
                    if (json.totlalCount > 0)
                    {
                        jsons = json.ToString();
                    }
                    else
                    {
                        jsons = @"[]";
                    }
                }
                else
                {
                    jsons = @"{success:false}";
                }
            }
            else
            {
                jsons = @"{success:false}";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void GetOrgnization()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            Orgnization query = new Orgnization();
            List<Orgnization> lstOrgnization = new List<Orgnization>();
            query.Start = Request.Form["start"];
            query.Limit = Request.Form["limit"];
            string OrgName = Request.Form["OrgName"];
            string RoleType = Request.Form["RoleType"];
            query.OrgName = OrgName;
            query.RoleType = RoleType;
            if (query.Start != null && query.Limit != null)
            {
                json.success = true;
                short shRet = OrgnizationAccess.Instance.GetOrgnizationPageList(query, ref lstOrgnization);

                foreach (Orgnization item in lstOrgnization)
                {
                    json.AddItem("ID", item.ID.ToString());
                    json.AddItem("OrgName", item.OrgName);
                    json.AddItem("RoleType", item.RoleType);
                    json.ItemOk();
                }
                int totalCount = 0;
                shRet = OrgnizationAccess.Instance.GetTotalCount(query, ref totalCount);
                json.totlalCount = totalCount;
                if (json.totlalCount > 0)
                {
                    jsons = json.ToString();
                }
                else
                {
                    jsons = @"[]";
                }
            }
            else
            {
                jsons = @"{success:false}";
            }

            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        public void BindRoleType()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            json.success = true;
            List<SysCombo> lstRoleType = new List<SysCombo>();
            ComboAccess.Instance.GetRoleTypes(ref lstRoleType);
            foreach (var row in lstRoleType)
            {
                json.AddItem("value", row.Value);
                json.AddItem("name", row.Name);
                json.ItemOk();
            }
            json.totlalCount = lstRoleType.Count();
            if (json.totlalCount > 0)
            {
                jsons = json.ToString();
            }
            else
            {
                jsons = @"[]";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
    }
}